package dice.sinanya.dice.system;

import dice.sinanya.dice.MakeNickToSender;
import dice.sinanya.dice.manager.imal.AtQq;
import dice.sinanya.entity.EntityGroupCensus;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.system.MessagesSystem;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nlpcn.commons.lang.util.StringUtil;

import java.util.ArrayList;

import static dice.sinanya.db.system.InsertBot.deleteBot;
import static dice.sinanya.db.system.SelectBot.selectBot;
import static dice.sinanya.system.MessagesSystem.*;
import static dice.sinanya.system.MessagesTag.*;
import static dice.sinanya.tools.checkdata.imal.LeaveGroup.leaveGroup;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.getinfo.SwitchBot.*;
import static dice.sinanya.tools.makedata.MakeMessages.deleteTag;
import static dice.sinanya.tools.makedata.Sender.sender;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;
import static dice.sinanya.windows.imal.HttpGet.sendGet;

/**
 * @author SitaNya
 * 日期: 2019-06-15
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明: 机器人控制类，如开关退群等
 */
public class Bot implements AtQq, MakeNickToSender {

    private final Logger log = LogManager.getLogger(Bot.class.getName());

    private final EntityTypeMessages entityTypeMessages;

    public Bot(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    /**
     * 开启
     */
    public void on() {
        ArrayList<String> qqList = getList(TAG_BOT_ON);
        for (String qq : qqList) {
            if (qq.equals(String.valueOf(loginInfo.getLoginId()))) {
                long groupId = entityTypeMessages.getFromGroup();
                if (groupId == 0) {
                    sender(entityTypeMessages, entitySystemProperties.getCantInPrivate(entityTypeMessages.getFromGroupString()));
                    return;
                }
                if (getBot(groupId)) {
                    sender(entityTypeMessages, entitySystemProperties.getBotAlreadyStart(entityTypeMessages.getFromGroupString()));
                } else {
                    botOn(groupId);
                    sender(entityTypeMessages, entitySystemProperties.getBotStart(entityTypeMessages.getFromGroupString()));
                }
            }
        }
    }

    /**
     * 关闭
     */
    public void off() throws InterruptedException {
        ArrayList<String> qqList = getList(TAG_BOT_OFF);

        for (String qq : qqList) {
            if (qq.equals(String.valueOf(loginInfo.getLoginId()))) {
                long groupId = entityTypeMessages.getFromGroup();
                if (groupId == 0) {
                    sender(entityTypeMessages, entitySystemProperties.getCantInPrivate(entityTypeMessages.getFromGroupString()));
                    return;
                }
                if (!getBot(groupId)) {
                    sender(entityTypeMessages, entitySystemProperties.getBotAlreadyStop(entityTypeMessages.getFromGroupString()));
                } else {
                    botOff(groupId);
                    sender(entityTypeMessages, entitySystemProperties.getBotStop(entityTypeMessages.getFromGroupString()));
                }
            }
        }
    }

    /**
     * 退群
     */
    public void exit() {
        ArrayList<String> qqList = getList(TAG_BOT_EXIT);
        for (String qq : qqList) {
            if (qq.equals(String.valueOf(loginInfo.getLoginId()))) {
                sender(entityTypeMessages, entitySystemProperties.getBotExit(entityTypeMessages.getFromGroupString()));
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    log.error(e.getMessage(), e);
                    Thread.currentThread().interrupt();
                }
                leaveGroup(entityTypeMessages, entityTypeMessages.getFromGroupString());
                deleteBot(entityTypeMessages.getFromGroup());
            }
        }
    }

    /**
     * 机器人信息
     */
    public void info() {
        String botInfo = entitySystemProperties.getBotInfo(entityTypeMessages.getFromGroupString());
        if (!botInfo.equals(NONE)) {
            botInfo = "\n" + botInfo;
        }
        EntityGroupCensus entityGroupCensus = selectBot();
        sender(entityTypeMessages, stringFormatById(devInfo.getBotInfo(), sendGet(devInfo.getUpdateAddress() + "/version")) + "\n目前供职于:\t" + entityGroupCensus.getGroupNum() + " 个群，其中 " + entityGroupCensus.getOnNum() + " 个群处于开启状态" + botInfo);
    }

    /**
     * 机器人更新日志
     */
    public void update() {
        ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i <= 20; i++) {
            result.add(UPDATE.get(i));
        }
        sender(entityTypeMessages, "当前版本(只显示最新的20条更新):\t" + MessagesSystem.VERSIONS + "\n" + StringUtil.joiner(result, "\n"));
    }

    private ArrayList<String> getList(String tag) {
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));

        ArrayList<String> qqList = getAtQqList(msg);

        if (qqList.isEmpty()) {
            qqList.add(String.valueOf(loginInfo.getLoginId()));
        }
        return qqList;
    }
}
